package dao;

import conexao.FabricaDeConexao;
import static dao.JDBCUsuarioDAO.getConecta;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.OpcoesMenu;

public class JDBCOpcoesMenuDao implements OpcoesMenuDAO {

    private static FabricaDeConexao conecta = new FabricaDeConexao();
    PreparedStatement ps;
    Connection conexao;
    Statement st;
    ResultSet rs;
    String query;

    public JDBCOpcoesMenuDao(FabricaDeConexao conecta,
            Connection conexao, Statement st, ResultSet rs, String query) {
        JDBCUsuarioDAO.setConecta(conecta);
        this.conexao = conexao;
        this.st = st;
        this.rs = rs;
        this.query = query;
    }

    @Override
    public void insereOpcoesMenu(OpcoesMenu novaOpcao) {
        String comandoSQL = "insert into opcoes_menu(name_prato,preco_prato,descricao)"
                + "values(?,?,?)";
        try {
            PreparedStatement p = (PreparedStatement) this.conexao
                    .prepareStatement(comandoSQL);
            p.setString(1, novaOpcao.getNomePrato());
            p.setString(2, novaOpcao.getPrecoPrato());
            p.setString(3, novaOpcao.getDescricao());

            p.executeQuery();

        } catch (Exception e) {
            System.out.println("NÃO DEU" + e);
        }
    }

    @Override
    public List<OpcoesMenu> listar() {
        String Sql = "SELECT * FROM opcoes_menu";
        List<OpcoesMenu> lista = null;
        try {
            lista = (List<OpcoesMenu>) this.conexao.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(JDBCOpcoesMenuDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lista;
    }
    
    public boolean ValidaPesquisa(String busca) {
         Connection con = getConecta().fazConexao();
         String sql = "SELECT * FROM opcoes_menu WHERE name_prato = '"+busca+"'";

        int sw = 0;
        try {

            PreparedStatement valores = (PreparedStatement) con.prepareStatement(sql);
            ResultSet result = (ResultSet) valores.executeQuery();

            if (result.next()) {
                if (result.getString(1) == null) {
                    sw = 0;
                } else {
                    sw = 1;
                }
            }
            valores.close();
        } catch (SQLException e) {
            System.out.println("Erro na inserçao de dados!" + e.getMessage());
        } finally {
            conecta.fechaConexao();
        }
        if (sw == 1) {
            return true;
        } else {
            return false;
        }
    }
}
